/*
 * Copyright 2010 David de Mingo
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.shalma.client.ui;

import java.util.ArrayList;
import java.util.List;

import org.shalma.Query;
import org.shalma.Query.FilterOperator;
import org.shalma.client.Callback;
import org.shalma.client.Persistence;
import org.shalma.client.ui.icon.ShalmaResources;
import org.shalma.model.Document;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;

public class DocumentView extends Composite implements AsyncCallback<Document> {

	private static final Style CSS = ShalmaResources.instance.documentViewCss();
	static {
		CSS.ensureInjected();
	}

	private Image loading;
	private Label nameView;
	private FlowPanel childrenView;
	private HTML bodyView;

	private PushButton homeButton;
	private PushButton upButton;

	private Document document;
	private Document homeDocument;

	public DocumentView() {

		FlowPanel container = new FlowPanel();

		HorizontalPanel controls = new HorizontalPanel();
		controls.addStyleName(CSS.document_controls());

		homeButton = new PushButton(new Image(ShalmaResources.instance.home()));
		homeButton.setEnabled(true);
		homeButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				update(homeDocument);
			}
		});
		controls.add(homeButton);

		upButton = new PushButton(new Image(ShalmaResources.instance.up()));
		upButton.setEnabled(false);
		upButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Persistence.instance.get(document.getParent().getId(),
						DocumentView.this);
			}
		});
		controls.add(upButton);

		container.add(controls);

		nameView = new Label();
		nameView.addStyleName(CSS.document_name());
		container.add(nameView);

		childrenView = new FlowPanel();
		container.add(childrenView);
		loading = new Image(ShalmaResources.instance.loading());
		container.add(loading);

		bodyView = new HTML();
		container.add(bodyView);

		initWidget(container);
	}

	public void update(String id) {

		// TODO loading

		Persistence.instance.get(id, new Callback<Document>() {
			@Override
			public void onSuccess(Document document) {
				update(document);
			}
		});
	}

	public void update(Document document) {

		loading.setVisible(false);

		if (homeDocument == null)
			homeDocument = document;
		this.document = document;

		boolean viewingHome = document.getId().equals(homeDocument.getId());
		homeButton.setEnabled(!viewingHome);
		upButton.setEnabled(!viewingHome);

		nameView.setText(document.getName());
		childrenView.clear();
		String body = document.getBody();
		bodyView.setHTML(body);
		// if (body != null)
		// bodyView.setHTML(SafeHtmlUtils.fromString(body));
		// else
		// bodyView.setHTML("");

		loading.setVisible(true);
		Query<Document> query = new Query<Document>("Ku$edfLFLn0=",
				FilterOperator.EQUAL, document.getId());
		Persistence.instance.get(query, new Callback<ArrayList<Document>>() {
			@Override
			public void onSuccess(ArrayList<Document> documents) {
				updateIndex(documents);
			}
		});
	}

	private void updateIndex(List<Document> documents) {

		loading.setVisible(false);

		for (Document document : documents) {

			final Document _document = document;

			Anchor anchor = new Anchor(document.getName());
			anchor.addStyleName(CSS.document_child());
			anchor.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					update(_document);
				}
			});

			childrenView.add(anchor);
		}
	}

	@Override
	public void onSuccess(Document document) {
		update(document);
	}

	@Override
	public void onFailure(Throwable caught) {
	}

	public interface Style extends CssResource {

		String document_controls();

		String document_name();

		String document_child();
	}
}
